﻿using Common.Library;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.ComponentModel;
using OnlineVideos.Data;
using System.Threading.Tasks;
using OnlineVideos.Entities;


namespace OnlineVideos.ViewModels
{
    public static class CreateCustomImage
    {
       static string fullpath = string.Empty;
    
       static Page Navigationpurpose = default(Page);
     
        public static void createFrontImage(Page currentpage,string imagename,string path="",string types="")
        {
            if (types == "Person")
            {
                //OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
                Navigationpurpose = currentpage;
                if (path != "")
                {
                    fullpath = path;
                    BitmapImage bi = new BitmapImage(new Uri(App.webinfo.PivotImage, UriKind.RelativeOrAbsolute));
                    bi.CreateOptions = BitmapCreateOptions.None;
                    bi.ImageOpened += bi_ImageOpened;
                }
                else
                {

                    Grid grid = new Grid();
                    BitmapImage Bitmap = new BitmapImage();
                    Bitmap.CreateOptions = BitmapCreateOptions.None;

                    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (IsolatedStorageFileStream local = myIsolatedStorage.OpenFile("personImage.jpg", FileMode.Open, FileAccess.Read))
                        {
                            Bitmap.SetSource(local);
                            local.Close();
                            local.Dispose();
                            myIsolatedStorage.Dispose();
                        }
                    }
                    Image ProductImage = new Image();

                    ProductImage.Source = Bitmap;
                    grid.Children.Add(ProductImage);
                    WriteableBitmap wbmp = new WriteableBitmap(grid, null);
                    using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        //if (!myIsolatedStorage.DirectoryExists(ResourceHelper.ProjectName))
                        //    myIsolatedStorage.CreateDirectory(ResourceHelper.ProjectName);
                        if (!myIsolatedStorage.DirectoryExists("Images/" + "PersonImages"))
                            myIsolatedStorage.CreateDirectory("Images/" + "PersonImages");
                        int persnid = 0;
                        if (ResourceHelper.AppName == Apps.Cricket_Videos.ToString())
                        {
                            persnid = Constants.personid;
                        }
                        else
                        {
                            int minpersonid = AppSettings.MinPersonID;
                            if (Task.Run(async () => await Constants.connection.Table<CastProfile>().Where(i => i.PersonID < minpersonid).OrderByDescending(i => i.PersonID).FirstOrDefaultAsync()).Result != null)
                                persnid = Task.Run(async () => await Constants.connection.Table<CastProfile>().Where(i => i.PersonID < minpersonid).OrderByDescending(i => i.PersonID).FirstOrDefaultAsync()).Result.PersonID + 1;
                            else
                                persnid = 2;
                        }
                        string personid = Convert.ToString(persnid);
                        using (IsolatedStorageFileStream local = myIsolatedStorage.OpenFile("Images" + "/" + "PersonImages" + "/" + personid + ".jpg", System.IO.FileMode.Create))
                        {
                            wbmp.SaveJpeg(local, 120, 110, 0, 100);
                            local.Close();
                            local.Dispose();
                            myIsolatedStorage.Dispose();
                        }
                    }

                    Navigationpurpose.NavigationService.GoBack();
                }
            }
            else if (types == "Story")
            {
                //OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
                Navigationpurpose = currentpage;
                if (path != "")
                {
                    fullpath = path;
                    BitmapImage bi = new BitmapImage(new Uri(App.webinfo.PivotImage, UriKind.RelativeOrAbsolute));
                    bi.CreateOptions = BitmapCreateOptions.None;
                    bi.ImageOpened += bi_ImageOpened;
                }
                else
                {

                    Grid grid = new Grid();
                    BitmapImage Bitmap = new BitmapImage();
                    Bitmap.CreateOptions = BitmapCreateOptions.None;

                    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (IsolatedStorageFileStream local = myIsolatedStorage.OpenFile("storyImage.jpg", FileMode.Open, FileAccess.Read))
                        {
                            Bitmap.SetSource(local);
                            local.Close();
                            local.Dispose();
                            myIsolatedStorage.Dispose();
                        }
                    }
                    Image ProductImage = new Image();

                    ProductImage.Source = Bitmap;
                    grid.Children.Add(ProductImage);
                    WriteableBitmap wbmp = new WriteableBitmap(grid, null);
                    using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        //if (!myIsolatedStorage.DirectoryExists(ResourceHelper.ProjectName))
                        //    myIsolatedStorage.CreateDirectory(ResourceHelper.ProjectName);
                        if (!myIsolatedStorage.DirectoryExists("Images/" + "storyImages/" + AppSettings.ShowUniqueID))
                            myIsolatedStorage.CreateDirectory("Images/" + "storyImages/" + AppSettings.ShowUniqueID);


                        using (IsolatedStorageFileStream local = myIsolatedStorage.OpenFile("Images" + "/" + "storyImages" + "/" + AppSettings.ShowUniqueID + "/" + imagename + ".jpg", System.IO.FileMode.Create))
                        {
                            Constants.newimage = AppSettings.ImageTitle + ".jpg";
                            wbmp.SaveJpeg(local, 400, 280, 0, 100);
                            local.Close();
                            local.Dispose();
                            myIsolatedStorage.Dispose();
                        }
                    }

                    Navigationpurpose.NavigationService.GoBack();
                }
            }
            else if (types == "Quiz")
            {
                //OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
                Navigationpurpose = currentpage;
                if (path != "")
                {
                    fullpath = path;
                    BitmapImage bi = new BitmapImage(new Uri(App.webinfo.PivotImage, UriKind.RelativeOrAbsolute));
                    bi.CreateOptions = BitmapCreateOptions.None;
                    bi.ImageOpened += bi_ImageOpened;
                }
                else
                {

                    Grid grid = new Grid();
                    BitmapImage Bitmap = new BitmapImage();
                    Bitmap.CreateOptions = BitmapCreateOptions.None;

                    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (IsolatedStorageFileStream local = myIsolatedStorage.OpenFile("quizImage.jpg", FileMode.Open, FileAccess.Read))
                        {
                            Bitmap.SetSource(local);
                            local.Close();
                            local.Dispose();
                            myIsolatedStorage.Dispose();
                        }
                    }
                    Image ProductImage = new Image();

                    ProductImage.Source = Bitmap;
                    grid.Children.Add(ProductImage);
                    WriteableBitmap wbmp = new WriteableBitmap(grid, null);
                    using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        //if (!myIsolatedStorage.DirectoryExists(ResourceHelper.ProjectName))
                        //    myIsolatedStorage.CreateDirectory(ResourceHelper.ProjectName);
                        if (!myIsolatedStorage.DirectoryExists("Images/" + "QuestionsImage"))
                            myIsolatedStorage.CreateDirectory("Images/" + "QuestionsImage");
                        if (!myIsolatedStorage.DirectoryExists("Images/" + "QuestionsImage/" + AppSettings.ShowUniqueID))
                            myIsolatedStorage.CreateDirectory("Images/" + "QuestionsImage/" + AppSettings.ShowUniqueID);

                        using (IsolatedStorageFileStream local = myIsolatedStorage.OpenFile("Images" + "/" + "QuestionsImage" + "/" + AppSettings.ShowUniqueID + "/" + imagename + ".jpg", System.IO.FileMode.Create))
                        {
                            wbmp.SaveJpeg(local, 200, 200, 0, 100);
                            local.Close();
                            local.Dispose();
                            myIsolatedStorage.Dispose();
                        }
                    }

                    Navigationpurpose.NavigationService.GoBack();
                }
            }
            else
            {
                Navigationpurpose = currentpage;
                if (path != "")
                {
                    fullpath = path;
                    BitmapImage bi = new BitmapImage(new Uri(App.webinfo.PivotImage, UriKind.RelativeOrAbsolute));
                    bi.CreateOptions = BitmapCreateOptions.None;
                    bi.ImageOpened += bi_ImageOpened;
                }
                else
                {

                    Grid grid = new Grid();
                    BitmapImage Bitmap = new BitmapImage();
                    Bitmap.CreateOptions = BitmapCreateOptions.None;

                    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (IsolatedStorageFileStream local = myIsolatedStorage.OpenFile("myImage.jpg", FileMode.Open, FileAccess.Read))
                        {
                            Bitmap.SetSource(local);
                            local.Close();
                            local.Dispose();
                            myIsolatedStorage.Dispose();
                        }
                    }
                    Image ProductImage = new Image();

                    ProductImage.Source = Bitmap;
                    grid.Children.Add(ProductImage);
                    WriteableBitmap wbmp = new WriteableBitmap(grid, null);
                    using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        //if (!myIsolatedStorage.DirectoryExists(ResourceHelper.ProjectName))
                        //    myIsolatedStorage.CreateDirectory(ResourceHelper.ProjectName);
                        if (!myIsolatedStorage.DirectoryExists("Images"))
                            myIsolatedStorage.CreateDirectory("Images");

                        using (IsolatedStorageFileStream local = myIsolatedStorage.OpenFile("Images" + "/" + imagename + ".jpg", System.IO.FileMode.Create))
                        {
                            wbmp.SaveJpeg(local, 130, 190, 0, 100);
                            local.Close();
                            local.Dispose();
                            myIsolatedStorage.Dispose();
                        }
                    }

                    Navigationpurpose.NavigationService.GoBack();
                }
            }
        }

        static void bi_ImageOpened(object sender, RoutedEventArgs e)
        {

            try
            {

                if (!Storage.DirectoryExists("/webslice/Images"))
                {
                    Storage.CreateDirectory("/webslice/Images");
                }

                Grid grid = new Grid();

                Canvas MainCanvas = new Canvas();
                MainCanvas.Width = 130;
                MainCanvas.Height = 190;

             
                TextBlock TitleText = new TextBlock();
                TitleText.Text = (Constants.WebSliceTitle.Length > 10) ? Constants.WebSliceTitle.Substring(0, 10) + ".." : Constants.WebSliceTitle; 
                TitleText.TextTrimming = TextTrimming.WordEllipsis;
                TitleText.FontFamily = new FontFamily("Segoe WP Semibold");
                TitleText.Foreground = new SolidColorBrush(Colors.White);
                TitleText.Margin = new Thickness(10, 0, 0, 0);
                TitleText.FontSize = 28;
                TitleText.Width = 130;

                BitmapImage ProductBitmap = new BitmapImage();
                ProductBitmap.CreateOptions = BitmapCreateOptions.None;
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream local = myIsolatedStorage.OpenFile("myImage.jpg", FileMode.Open, FileAccess.Read))
                    {
                        ProductBitmap.SetSource(local);
                    }
                }
                Image ProductImage = new Image();
                ProductImage.SetValue(Canvas.TopProperty, (double)32);
                ProductImage.Source = ProductBitmap;

                Image FavImage = new Image(); 
                FavImage.Height = 16;
                FavImage.Width =16;
                FavImage.Source = (sender as BitmapImage) as ImageSource;
                FavImage.SetValue(Canvas.LeftProperty, (double)105);
                FavImage.SetValue(Canvas.TopProperty, (double)40);

                TextBlock SelectedText = new TextBlock();
                SelectedText.Text = (App.webinfotable.SelectedText.Length > 10) ? App.webinfotable.SelectedText.Substring(0, 10) + ".." : App.webinfotable.SelectedText;
                SelectedText.TextTrimming = TextTrimming.WordEllipsis;
                SelectedText.Margin = new Thickness(10, 0, 0, 0);
                SelectedText.FontFamily = new FontFamily("Segoe WP Semibold");
                SelectedText.Foreground = new SolidColorBrush(Colors.White);
                SelectedText.FontSize = 22;
                SelectedText.SetValue(Canvas.TopProperty, (double)162);
                SelectedText.Width = 130;

                MainCanvas.Children.Add(TitleText);
                MainCanvas.Children.Add(ProductImage);
                MainCanvas.Children.Add(FavImage);
                MainCanvas.Children.Add(SelectedText);
                grid.Children.Add(MainCanvas);
               
                       
                WriteableBitmap wbmp = new WriteableBitmap(grid, null);
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (var local = myIsolatedStorage.OpenFile(fullpath, System.IO.FileMode.OpenOrCreate))
                    {
                        wbmp.SaveJpeg(local, 130, 190, 0, 100);
                        local.Close();
                        local.Dispose();
                        myIsolatedStorage.Dispose();
                    }
                }
                Constants.WebSliceImagePath = fullpath;
                Navigationpurpose.NavigationService.GoBack();

            }
            catch (Exception ex)
            {

            }
        }
    }
}
